package cn.com.czpb.client.module.personal.acticity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
//import android.webkit.JsResult;
//import android.webkit.SslErrorHandler;
//import android.webkit.WebChromeClient;
//import android.webkit.WebSettings;
//import android.webkit.WebView;
//import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.BindView;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.MToolBarActivity;

/**
 * Created by 猪猪侠 on 2017/11/16.
 */

public class HtmlSaoActivity extends MToolBarActivity {
    @BindView(R.id.webview_id)
    WebView webView;
    @BindView(R.id.tv_id)
    TextView tv_id;
    @BindView(R.id.ll_back)
    LinearLayout ll_back;
    @Override
    public void initView() {
        super.initView();
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_sao_html;
    }

    @Override
    public void initData() {
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        if (url.contains("http")){

        }

        WebSettings webSettings = webView.getSettings();
        webSettings.setDisplayZoomControls(false);
        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
        webSettings.setAllowFileAccess(true); // 允许访问文件
        // 设置可以支持缩放
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        //扩大比例的缩放
        webSettings.setUseWideViewPort(true);
        //自适应屏幕
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setLoadWithOverviewMode(true);

        webView.setWebViewClient(new WebViewClient(){
            @Override

            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);

                //如果不需要其他对点击链接事件的处理返回true，否则返回false

                return true;

            }
//            @Override
//            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//                handler.proceed();
//            }
        });
        webView.setWebChromeClient(new MyWebChromeClient());


        if (url.contains("http")){
            webView.setVisibility(View.VISIBLE);
            tv_id.setVisibility(View.GONE);
            webView.loadUrl(url);
        }else{
            tv_id.setText(url);
            webView.setVisibility(View.GONE);
            tv_id.setVisibility(View.VISIBLE);
        }

    }
    public class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult jsResult) {
            final JsResult finalJsResult = jsResult;
            new AlertDialog.Builder(view.getContext()).setMessage(message).setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finalJsResult.confirm();
                }
            }).setCancelable(false).create().show();
            return true;
        }
    }

    @Override
    public void finish() {
        super.finish();
        webView.loadUrl("about:blank");
    }
}
